package com.xul.crowd.controller;

import com.xul.crowd.entity.Auth;
import com.xul.crowd.entity.Role;
import com.xul.crowd.service.AdminService;
import com.xul.crowd.service.AuthService;
import com.xul.crowd.service.RoleService;
import com.xul.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author xul
 * @create 2021-07-25 17:19
 */

@Controller
public class AssignController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private AuthService authService;

    /**
     * 将菜单和权限关联的数据保存到数据库
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping("assign/do/menu/assign/auth.json")
    public ResultEntity<String> saveMenuAuthRelationship(@RequestBody Map<String, List<Integer>> map) {
        authService.saveMenuAuthRelationship(map);
        return ResultEntity.successWithData();
    }

    /**
     * 根据中间表用menuId查询出所有的authId
     *
     * @param menuId
     * @return
     */
    @ResponseBody
    @RequestMapping("assign/get/assigned/auth/id/by/menu/id.json")
    public ResultEntity<List<Integer>> getAssignedAuthByMenuId(@RequestParam Integer menuId) {
        List<Integer> authIdList = authService.getAssignedAuthIdByMenuId(menuId);
        return ResultEntity.successWithData(authIdList);
    }

    /**
     * 将角色和权限关联的数据保存到数据库
     *
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping("assign/do/role/assign/auth.json")
    public ResultEntity<String> saveRoleAuthRelationship(@RequestBody Map<String, List<Integer>> map) {
        authService.saveRoleAuthRelationship(map);
        return ResultEntity.successWithData();
    }

    /**
     * 根据中间表用roleId查询出所有的authId
     *
     * @param roleId
     * @return
     */
    @ResponseBody
    @RequestMapping("assign/get/assigned/auth/id/by/role/id.json")
    public ResultEntity<List<Integer>> getAssignedAuthIdByRoleId(@RequestParam Integer roleId) {
        List<Integer> authIdList = authService.getAssignedAuthIdByRoleId(roleId);
        return ResultEntity.successWithData(authIdList);
    }


    /**
     * 查询所以的权限
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("assign/get/all/auth.json")
    public ResultEntity<List<Auth>> getAllAuth() {
        List<Auth> authList = authService.getAllAuth();
        return ResultEntity.successWithData(authList);
    }

    /**
     * 管理员实现对角色的分配 查询出已经分配和未分配的角色
     *
     * @param adminId
     * @param modelMap
     * @return
     */
    @RequestMapping("assign/to/assign/role/page")
    public String toAssignRolePage(@RequestParam Integer adminId,
                                   ModelMap modelMap) {
        // 1.查询已经分配的角色
        List<Role> assignRoleList = roleService.getAssignRole(adminId);
        // 2.查询没有分配的角色
        List<Role> unAssignRoleList = roleService.getUnAssignRole(adminId);
        // 3.存入模型
        modelMap.addAttribute("assignRoleList", assignRoleList);
        modelMap.addAttribute("unAssignRoleList", unAssignRoleList);
        return "role/assign-role";
    }

    /**
     * 管理员实现对角色的分配 保存选中的角色到数据库(提交表单)
     *
     * @param adminId
     * @param pageNum
     * @param keyword
     * @param roleIdList
     * @return
     */
    @RequestMapping("assign/do/role/assign.html")
    public String saveAdminRoleRelationship(@RequestParam("adminId") Integer adminId,
                                            @RequestParam("pageNum") Integer pageNum,
                                            @RequestParam("keyword") String keyword,
                                            // 设置required = false 表示这个请求参数不是必须的  我们允许用户在页面上取消所有分配角色再提交表单 所以可以不提供roleIdList请求参数
                                            @RequestParam(value = "roleIdList", required = false) List<Integer> roleIdList) {
        adminService.saveAdminRoleRelationship(adminId, roleIdList);
        return "redirect:/admin/get/page.html?pageNum=" + pageNum + "&keyword=" + keyword;
    }
}
